package com.example.filesynsystem.config;



import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AwsConfig {
    @Value("${s3.awsAccessKey}")
    private String accessKey;

    @Value("${s3.awsSecurityKey}")
    private String securityKey;

    @Value("${s3.serviceEndpoint}")
    private String serviceEndpoint;

    @Value("${s3.signingRegion}")
    private String signingRegion;

    @Bean
    public AmazonS3 amazonS3() {
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, securityKey);
        EndpointConfiguration endpoint = new EndpointConfiguration(serviceEndpoint, signingRegion);
        ClientConfiguration ccfg = new ClientConfiguration().withUseExpectContinue(true);
        AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                .withClientConfiguration(ccfg)
                .withEndpointConfiguration(endpoint)
                .withPathStyleAccessEnabled(true)
                .build();
        return amazonS3;
    }
}
